﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Entities.BaseClasses;
using SMS.Common.Silverlight;
using System.ComponentModel;
using System.Windows.Data;
using System.Collections.Generic;
using SMS.Entities;

namespace SMS.ViewModel.Master
{
    public class CategoryViewModel:BaseNotify
    {
        SMS.Model.CategoryMasterModel _model;

        public CategoryViewModel()
        {
            SearchCommand = new DelegateCommand(Search_Click, null);
            SaveCommand = new DelegateCommand(Save_Click, null);
            NewCommand = new DelegateCommand(New_Click, null);
            if (!DesignerProperties.IsInDesignTool)
            {
                _model = new Model.CategoryMasterModel();
                _model.OnServiceError += new Action<Entities.SMSException>(_model_OnServiceError);
                _model.OnSaveCategoryCompleted += new Action<int>(_model_OnSaveCategoryCompleted);
                _model.OnSearchCompleted += new Action<System.Collections.ObjectModel.ObservableCollection<Entities.Category>>(_model_OnSearchCompleted);
            }
        }

        void _model_OnSearchCompleted(System.Collections.ObjectModel.ObservableCollection<Entities.Category> obj)
        {
            UserMessage = obj.Count + " Category found";
            CategoryList = new List<Entities.Category>(obj);
            PagedCategoryList = new PagedCollectionView(CategoryList);
        }

        void _model_OnSaveCategoryCompleted(int obj)
        {
            UserMessage = "Category saved successfully.";
            SelectedMaster.Category_ID = obj;
        }

        void _model_OnServiceError(Entities.SMSException obj)
        {
            ErrorMessage = obj.Message;
        }

       
        private List<Category> _CategoryList;

        public List<Category> CategoryList
        {
            get { return _CategoryList; }
            set { _CategoryList = value; OnPropertyChanged("CategoryList"); }
        }

        private Category _SelectedCategory;

        public Category SelectedCategory
        {
            get { return _SelectedCategory; }
            set { _SelectedCategory = value; OnPropertyChanged("SelectedCategory"); }
        }

        private Category _SelectedMaster;

        public Category SelectedMaster
        {
            get { return _SelectedMaster; }
            set { _SelectedMaster = value; OnPropertyChanged("SelectedMaster"); }
        }

        private PagedCollectionView _PagedCategoryList;

        public PagedCollectionView PagedCategoryList
        {
            get { return _PagedCategoryList; }
            set { _PagedCategoryList = value; OnPropertyChanged("PagedCategoryList"); }
        }

        private string _SearchText;

        public string SearchText
        {
            get { return _SearchText; }
            set { _SearchText = value; OnPropertyChanged("SearchText"); }
        }

        private string  _Text;

        public string  Text
        {
            get { return _Text; }
            set { _Text = value; OnPropertyChanged("Text"); }
        }
        

        public ICommand SearchCommand { get; private set; }

        public void Search_Click(object param)
        {            
            _model.SearchCategory(SearchText);

            UserMessage = "Searching customer...";
        }

        public ICommand SaveCommand { get; set; }

        public void Save_Click(object param)
        {
            if (SelectedMaster != null)
            {
                
                _model.SaveCategory(SelectedMaster);

                UserMessage = "Saving customer...";
            }
            else
            {
                ErrorMessage = "No Customer to save";
            }
        }

        public ICommand NewCommand { get; set; }

        public void New_Click(object param)
        {
            if (CategoryList == null)
                CategoryList = new List<Category>();
            var k = new Category() { IsLive = true };
            CategoryList.Add(k);
            PagedCategoryList = new PagedCollectionView(CategoryList);
            PagedCategoryList.MoveToLastPage();
            SelectedMaster = k;
        }
    }
}

